﻿/**********************************************************************
*创建人：蔡水明
*创建时间：2018/11/15 16:43:12
*说明：<全局静态参数和方法>
*模板文件：C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ProjectTemplates\CSharp\Windows Root\Windows\2052\WindowsApplication
/**********************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebSocketSharp.Server;
using Newtonsoft.Json;
using System.Collections;

namespace WssServer
{
    /// <summary>
    /// 全局变量
    /// </summary>
    public static class GVar
    {
        public static string ver = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
        public static WebSocketSessionManager wsServer = null;
        //当前与自己连接的SessionID
        public static string SessionID = string.Empty;       

        /// <summary>
        /// 配置文件路径
        /// </summary>
        public static string CfgPath = string.Empty;
      
   
        //在线人员
        public static List<Users> OnLineUser = new List<Users>();
        /// <summary>
        /// 设置人员在线
        /// </summary>
        /// <param name="du"></param>
        public static void SetOnLine(Users du)
        {
            OnLineUser.RemoveAll(x => x.userId == du.userId);
            OnLineUser.Add(du);
        }
        /// <summary>
        /// 设置人员下线
        /// </summary>
        /// <param name="Id"></param>
        public static long SetOffLine(string Id)
        {
            var us = OnLineUser.Find(x => x.ID == Id);
            OnLineUser.RemoveAll(x => x.ID == Id);
            if (us != null)
                return us.userId;
            else return -1;
        }
        /// <summary>
        /// 设置人员下线
        /// </summary>
        /// <param name="Id"></param>
        public static void SetOffLine(Users du)
        {
            OnLineUser.RemoveAll(x => x.userId == du.userId);
        }

        /// <summary>
        /// 初始化信息
        /// </summary>
        public static void IniVar()
        {
            CfgPath = Path.Combine(Environment.CurrentDirectory, "Cfg");
            if (!Directory.Exists(CfgPath)) Directory.CreateDirectory(CfgPath);
            StartUp();
        }
        /// <summary>
        /// 修改程序在注册表中的键值
        /// </summary>        
        private static void StartUp()
        {
            string path = Application.StartupPath;
            string keyName = path.Substring(path.LastIndexOf("\\") + 1);

            Microsoft.Win32.RegistryKey Rkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            if (Rkey == null)
            {
                Rkey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
            }
            Rkey.SetValue(keyName, Application.ExecutablePath);

        }
        #region 显示消息
        public static void ShowMessage(string content)
        {
            if (content != null)
                MessageBox.Show(content, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        public static DialogResult ShowYNCMessage(string content)
        {
            return MessageBox.Show(content, "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
        }
        #endregion            
        public static HttpServer httpsv;
    }
}
